﻿using System;
using System.Collections.Generic;
using System.Drawing.Design;
using System.Linq;
using System.Windows.Forms.Design;
using DslModeling = global::Microsoft.VisualStudio.Modeling;
using devtm.AutoMapper.CustomCode.TemplateAdd;
using devtm.Editor.TypeSelector;


namespace devtm.AutoMapper
{

    public class ListeTypeTypeEditor : UITypeEditor
    {

        protected DslModeling.Store Store;
        protected TypeElement Instance;


        /// <summary>
        /// Edits the specified object's value using the editor style indicated by the <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"/> method.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that can be used to gain additional context information.</param>
        /// <param name="provider">An <see cref="T:System.IServiceProvider"/> that this editor can use to obtain services.</param>
        /// <param name="value">The object to edit.</param>
        /// <returns>
        /// The new value of the object. If the value of the object has not changed, this should return the same object it was passed.
        /// </returns>
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {

            IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc != null)
            {

                TypeElementShape g = context.Instance as TypeElementShape;
                Store = g.Store;
                Instance = g.Subject as TypeElement;


                WindowsSelectType frm = null;

                try
                {
                    frm = new WindowsSelectType();
                }
                catch (System.Exception)
                {
                    return value;
                }
                

                Type t = null;
                try
                {
                    t = Type.GetType((string)value);
                }
                catch (Exception) { }

                HashSet<string> namespaces = Instance.MappingModel.GetNamespaces;
                List<Type> lst = ContainerTypes.GetFromReference(Store, namespaces).ToList();
                lst.AddRange(ContainerTypes.GetFromSolution<Object>(Store, namespaces));

                frm.SetList(lst);

                try
                {
                    if (frm.ShowDialog()== System.Windows.Forms.DialogResult.OK)
                    {
                        Type t1 = frm.SelectedItem;
                        value = t1.AssemblyQualifiedName;                        
                    }

                }
                catch (System.Exception)
                {
                    
                    
                }
                

            }

            return value;

        }



        public override bool IsDropDownResizable
        {
            get
            {
                return false;
            }
        }

        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
            //return base.GetEditStyle(context);
        }

    }
}
